package com.wgj.android.accesslibility.service;

import android.accessibilityservice.AccessibilityService;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.wgj.android.accesslibility.AccessibilityHelper;

/**
 * Created by Administrator on 2017/11/11.
 */

public     class WXShareAccesslibilityService extends AccessibilityService {


    private static boolean  isOnStart=false;
    private AccessibilityNodeInfo nodeInfo;

    @Override
    protected void onServiceConnected() {
        Log.e("TTTTT","onServiceConnected");
        super.onServiceConnected();


    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("TTTTT","onCreate");

    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        if(!isOnStart){
            return;
        }
        nodeInfo=getRootInActiveWindow();
        if(event.getEventType()==AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED|| event.getEventType()==AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED){
            String className=event.getClassName().toString();
            Log.i("TTTTTTT",className);
            switch (className){
                case "com.tencent.mm.ui.LauncherUI":
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    nodeInfo=getRootInActiveWindow();
                    if(nodeInfo!=null){
                        AccessibilityHelper.performClick(AccessibilityHelper.findNodeInfosByText(nodeInfo,"我"));
                    }
                  try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    nodeInfo=getRootInActiveWindow();
                    if(nodeInfo!=null){
                        AccessibilityHelper.performClick(AccessibilityHelper.findNodeInfosByText(nodeInfo,"设置"));
                    }
                 break;
                case "com.tencent.mm.plugin.setting.ui.setting.SettingsUI":
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    AccessibilityHelper.performClick(AccessibilityHelper.findNodeInfosByText(nodeInfo,"通用"));

                    break;
                case "com.tencent.mm.plugin.setting.ui.setting.SettingsAboutSystemUI":
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if( !AccessibilityHelper.performClick(AccessibilityHelper.findNodeInfosByText(nodeInfo,"功能"))){
                                AccessibilityHelper.performClick(AccessibilityHelper.findNodeInfosById(nodeInfo,"com.tencent.mm:id/a9h"));
                            }
                        }
                    },500);
                    break;
                case "com.tencent.mm.plugin.setting.ui.setting.SettingsPluginsUI":
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    AccessibilityHelper.performClick(AccessibilityHelper.findNodeInfosByText(nodeInfo,"群发助手"));
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    AccessibilityNodeInfo nodeInfo=getRootInActiveWindow();
                    if(nodeInfo!=null){
                        AccessibilityHelper.performClick(AccessibilityHelper.findNodeInfosByText(nodeInfo,"开始群发"));
                    }

                    break;
                case "com.tencent.mm.plugin.masssend.ui.MassSendHistoryUI":
                    AccessibilityHelper.performClick(AccessibilityHelper.findNodeInfosByText(getRootInActiveWindow(),"新建群发"));
                    isOnStart=false;
                    break;
            }

        }



    }

    @Override
    public void onInterrupt() {
        Log.e("TTTTT","onInterrupt");
    }


    public  static  void  start(){
        isOnStart=true;
    }

}
